/**
 * Created by DevilsEyes on 2016/3/22.
 */
define(['css!components/anime/anime.css'], function () {
    'use strict';

    var $root = $('#animation');

    var anime = {
        cartAdd: function (object) {

            var $cart = object.cart;
            var $product = object.product;
            var callback = object.callback;
            var $spot = $('<div class="anime-spot">');
            var timer = null;
            var tMax = 300;
            var t = 0;
            var dt = 20;

            $root.append($spot);

            var pf = $product.offset();
            var cf = $cart.offset();

            pf = {
                top: pf.top + 50,
                left: pf.left + 50
            };

            cf = {
                top: cf.top + 20,
                left: cf.left + 20
            };

            var kx = (cf.left - pf.left) / tMax;
            var ky = (cf.top - pf.top) / (tMax * tMax);

            $spot.offset({
                top: pf.top,
                left: pf.left
            });

            timer = setInterval(function () {
                t += dt;
                $spot.offset({
                    top: pf.top + ky * t * t,
                    left: pf.left + kx * t
                });
                if (t == (tMax - 100)) {
                    $cart.animate({
                        width: 30,
                        height: 30,
                        right: 25,
                        bottom: 75
                    }, 100);
                }
                if (t >= tMax) {
                    clearInterval(timer);
                    $spot.remove();
                    $cart.animate({
                        width: 40,
                        height: 40,
                        right: 20,
                        bottom: 70
                    }, 100, callback);
                }
            }, dt);

        }
    };

    return anime;

});